local cwh_diy = require "packages.sanshe.pkg.utility.cwh_diy"

local skel = fk.CreateSkill({
    name = "sj8__xilong",
    tags = {cwh_diy.ReadySkill},
  })
  
  Fk:loadTranslationTable{
    ["sj8__xilong"] = "洗龙",
    [":sj8__xilong"] = "<b><a href='cwh_diy_ready_skill'>蓄势技</a></b>，回合结束时，你可以视为依次使用【火攻】【过河拆桥】【借刀杀人】。你可以用流程中出现的【杀】蓄势或获得之。",
  
    ["#sj8__xilong_1"] = "获得",
    ["#sj8__xilong_2"] = "蓄势",
    ["#sj8__xilong_slash"] = "洗龙：你可以获得 %arg 或将之弃置以蓄势",
  
    ["$sj8__xilong1"] = "",
    ["$sj8__xilong2"] = "",
  }

  local xilong_ask = function (player,ids)
    local room = player.room
    for _, id in ipairs(ids) do
      local card = Fk:getCardById(id)
      if card and card.trueName == "slash" then
        local ask = {"#sj8__xilong_1"}
        if cwh_diy.isReadying(player,skel.name) then
          table.insert(ask,"#sj8__xilong_2")
        end
        table.insert(ask,"Cancel")
        local choice = room:askToChoice(player,{
          choices = ask,
          prompt = "#sj8__xilong_slash:::"..card:toLogString(),
          skill_name = skel.name,
        })
        local area = room:getCardArea(card)
        local reason = (area == Card.PlayerHand or area == Card.PlayerEquip) and fk.ReasonPrey or fk.ReasonJustMove
        if choice == "#sj8__xilong_1" then
          room:obtainCard(player,card,true,reason,player,skel.name)
        end
        if choice == "#sj8__xilong_2" then
          if area ~= Card.DiscardPile then
            room:moveCardTo(card,Card.DiscardPile,nil,fk.ReasonDiscard,skel.name,nil,true,player)
          end
          cwh_diy.changeReady(player,skel.name)
        end
        room:syncDrawPile()
      end
    end
  end

  skel:addEffect(fk.TurnEnd, {
    anim_type = "defensive",
    can_trigger = function(self, event, target, player, data)
      return player:hasSkill(skel.name) and target == player
    end,
    on_cost = function (self, event, target, player, data)
      return player.room:askToSkillInvoke(player,{
        skill_name = skel.name,
      })
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      for _, name in ipairs({"fire_attack","dismantlement","collateral"}) do
        if not player.dead then
          room:askToUseVirtualCard(player,{
            name = name,
            skill_name = skel.name,
            cancelable = false,
          })
        end
      end
    end,
  })

skel:addEffect(fk.CardShown, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name,true) then
      local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard,nil,99)
      return parent and parent.data.card.skillName == skel.name
    end
  end,
  on_refresh = function (self, event, target, player, data)
    xilong_ask(player,data.cardIds)
  end,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name,true) and not data.card:isVirtual() then
      local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard,nil,99)
      return parent and parent.data.card.skillName == skel.name
    end
  end,
  on_refresh = function (self, event, target, player, data)
    xilong_ask(player,{data.card.id})
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(skel.name,true) then
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonDiscard and move.skillName ~= skel.name then
          local parent = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard,nil,99)
          return parent and parent.data.card.skillName == skel.name
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local ids = {}
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        table.insert(ids,info.cardId)
      end
    end
    xilong_ask(player,ids)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  cwh_diy.setReady(player,skel.name)--暂定不因获得/失去技能而重置
end)


return skel